<?php
session_start();
require_once('../config.php');
$avatar_redir = IMAGE_FOLDER.'/avatar/';
$avatar_default = './style/default/no_avatar.jpg';

?>
<div class="close_listicon" onclick="closethis();"><img src="./style/default/close.png" border="0" /></div>
<?php
if(isset($_SESSION[sess_login]))
{
	$flag = load_form_friendlist();
	if($flag !=''){
		echo $flag;
	}
	else{
		echo 'No friend... You have to add friend';
	}
}
else{
	echo "<br />Wrong variables <br />";
}

function load_form_friendlist(){
	global $avatar_redir, $avatar_default;
	$get_friend = '';
	$array_list_friend ='';
	$i = 0;
	$selected_checkbox = '';
	$selected_li = '';			

	include("../connect.php");
	$sql = 	"SELECT `member`";
	$sql .= " FROM `user_groupview`";
	$sql .= " WHERE `owner` = ".$_SESSION[sess_login]['id']." AND `groupname` = '".name_db_friendlist."'";
	$result = mysql_query($sql, $conn);
	if(mysql_num_rows($result)==1){
	  $row = mysql_fetch_row($result);
	  $get_friend = str_replace(':',',',$row[0]);
	  
	if($get_friend!=''){
		$merg_sql = "SELECT a.`id`, a.`username`, b.`avatar`";
		$merg_sql .= " FROM `user_account` a,`user_info` b";
		$merg_sql .= " WHERE a.`id` = b.`id` AND a.`status` = '1' AND  a.`id` IN (".$get_friend.")";
		$merg_sql .= " ORDER BY a.`username` ASC";
		$result2 = mysql_query($merg_sql, $conn);
		if (mysql_num_rows($result2)!=0){
			while ($row2 = mysql_fetch_row($result2)){ 
				$array_list_friend[] = $row2;
			}
		}
		mysql_free_result($result2);			
	}

  mysql_free_result($result);	
  mysql_close($conn);
  $var_str_return='';
	if($array_list_friend!=''){
		$i = ($i-1);
		$var_str_return .= '<div class="form_save_add_member uix-button" onclick="friend_to_album();"> &nbsp; DONE &nbsp; </div>';
		$var_str_return .= '<ul class="style_user_notfriend">';
		foreach($array_list_friend as $each_friend => $vallist){
/*			if($get_friend!=''){
				if($get_friend[$i] == $vallist['0']){
					$selected_checkbox = 'checked="checked"';
					$selected_li = 'class="selected"';
					if($i != 0)	$i--;
				}
				else {
					$selected_checkbox = '';
					$selected_li = '';			
				}
			}
*/			$var_str_return .= '<li '.$selected_li.'>';
			if(!file_exists($avatar_redir.$vallist['2']) || $vallist['2'] == ''){
				$link_image = $avatar_default;
			} else {
				$link_image = $avatar_redir.$vallist['2'];
			}
			$var_str_return .= '<div class="checkbox_friend" id="bt_'.$vallist['0'].'" align="right"><input type="checkbox" name="select_friend" '.$selected_checkbox.' style="display:none;" value="'.$vallist['0'].'" alt="'.$vallist['1'].'"/></div>';
			$var_str_return .= '<div class="avatar"><img src="'. $link_image .'"></div>';
			$var_str_return .= '<div align="center">'. $vallist['1'] .'</div>';
			$var_str_return .= '</li>';
		}
		$var_str_return .= '</ul>';
	}
	
	$var_str_return .= '<script>
	
	$("#loadlistfriend ul.style_user_notfriend li").click(
		function() { 
			if($(this).find("input:checkbox").is(":checked")){
				$(this).find("input:checkbox").removeAttr("checked").parent().parent().removeClass("selected");
			}
			else {
				$(this).find("input:checkbox").attr("checked","checked").parent().parent().addClass("selected");
			}
		}
	);
	

	</script>';
	
	return $var_str_return;
	}
	else {
	  return '';
	}

}
?>
